var service = require('./service')
const _ = require('lodash')
var Page = require('./model').Page
var PageController = {
  detail: function (req, res) {
    console.log(req.params)
    service.detailPage(req.params.id, function (datas) {
      res.send(datas)
    }, function (err) {
      console.error(err)
    })
  },

  add: function (req, res) {
    var pages = Array.isArray(req.body) ? req.body : [req.body]
    pages = _.reduce(pages, function (a, item) {
      item.dbInfo = {
        userId: 0,
        groupId: 0
      }
      a.push(new Page(item))
      console.log('yunsuanzhong', a)
      return a
    }, [])
    console.log(pages)
    service.addPage(pages, function (datas) {
      res.send(datas)
    }, function (err) {
      console.error(err)
    })
  },

  list: function (req, res) {
    service.listPage(function (datas) {
      res.send(datas)
    }, function (err) {
      console.error(err)
    })
  },

  remove: function (req, res) {
    var id = req.params.id
    if (!id) {
      console.error('id is null')
      res.end('id is null')
    }
    service.softDeletePage(id, function (datas) {
      res.send(datas)
    }, function (err) {
      console.error(err)
    })
  },

  addMatirials: function (req, res) {

  },

  change: function (req, res) {
    var page = new Page(req.body)
    var id = req.params.id
    if (!id) {
      console.error('id is null')
      res.end('id is null')
    }
    service.changePageContent(id, page, function (datas) {
      res.send(datas)
    }, function (err) {
      console.error(err)
    })
  },

  genarateAssets: function (req, res) {
    service.genarateAssets(function () {
      res.send({'status': 'success'})
    }, function () {
      res.send({'status': 'error please contract you admin'})
    })
  },

  genaratePage: function (req, res) {
    var pageId = req.params.id
    service.genaratePage(pageId, function () {
      res.send({'status': 'success'})
    }, function () {
      res.send({'status': 'error please contract you admin'})
    })
  }
}
module.exports = PageController
